package com.wzcl.app.controller;


import com.wzcl.app.dao.model.CourseTeacher;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.CourseTeacherDto;
import com.wzcl.app.model.vo.CourseTeacherVo;
import com.wzcl.app.service.CourseTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/courseTeacher")
@Api(value = "课程教师管理Controller", tags = {"课程教师管理接口"})
public class CourseTeacherController {

    @Resource
    private CourseTeacherService service;

    @ApiOperation("添加课程老师")
    @PostMapping("/add")
    public RVO<CourseTeacherVo> addCourseTeacher(@RequestBody @Valid CourseTeacherDto courseTeacherDto){
        CourseTeacher courseTeacher = service.addCourseTeacher(courseTeacherDto);
        return RVO.success(CourseTeacherVo.create(courseTeacher));
    }

    @ApiOperation("删除课程老师")
    @PostMapping("/del")
    public RVO<CourseTeacherVo> delCourseTeacher(@RequestBody CourseTeacherDto courseTeacherDto){
        CourseTeacher courseTeacher = service.delCourseTeacherById(courseTeacherDto.getId());
        return RVO.success(CourseTeacherVo.create(courseTeacher));
    }

    @ApiOperation("获取课程老师")
    @PostMapping("/get")
    public RVO<CourseTeacherVo> getCourseTeacher(@RequestBody CourseTeacherDto courseTeacherDto){
        CourseTeacher courseTeacher = service.getCourseTeacherById(courseTeacherDto.getId());
        if(courseTeacher !=null){
            return RVO.success(CourseTeacherVo.create(courseTeacher));
        }
        return RVO.success(null);
    }

    @ApiOperation("课程老师list")
    @PostMapping("/getList")
    public RVO<List<CourseTeacherVo>> getCourseTeacherList(@RequestBody CourseTeacherDto courseTeacherDto){
        List<CourseTeacher> list = service.getCourseTeacherList(courseTeacherDto);
        if (list != null && list.size() > 0) {
            List<CourseTeacherVo> collect = list.stream().map(CourseTeacherVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("课程老师列表page")
    @PostMapping("/getPage")
    public  RVO<List<CourseTeacherVo>> getCourseTeacherPage(@RequestBody CourseTeacherDto courseTeacherDto){
        MyPage<CourseTeacher> page = service.getCourseTeacherPage(courseTeacherDto);
        if (page != null && page.getContent().size()>0) {
            List<CourseTeacherVo> collect = page.getContent().stream().map(CourseTeacherVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return RVO.success(null);
    }
}
